﻿using BookManege.entity;
using BookManege.service;


namespace BookManege.controller
{
    public partial class borrowManege : Form
    {
        private BorrowManegeService borowManegeService;
        private UserManegeService userManegeService;
        private int pageNum;
        private int pageSize;

        public borrowManege()
        {
            InitializeComponent();
            this.borowManegeService = new BorrowManegeService();
            this.userManegeService = new UserManegeService();
            this.pageNum = 1;
            this.pageSize = 10;
        }

        private void borrowManege_Load(object sender, EventArgs e)
        {
            GetBoorowList();
            //下拉框绑定用户名
            DataGridViewComboBoxColumn dataGridViewComboBoxColumn = dataGridView1.Columns["user_id"] as DataGridViewComboBoxColumn;
            dataGridViewComboBoxColumn.DataSource = userManegeService.GetAllUser();
            dataGridViewComboBoxColumn.DisplayMember = "user_name";
            dataGridViewComboBoxColumn.ValueMember = "id";

            comboBox2.DataSource = userManegeService.GetAllUser();
            comboBox2.DisplayMember = "user_name";
            comboBox2.ValueMember = "id";

            textBox3.Text = pageNum.ToString();

            if (pageNum == 1)
            {
                linkLabel1.Enabled = false;
            }
        }
        public void GetBoorowList()
        {
            dataGridView1.DataSource = borowManegeService.GetBoorowList(pageNum, pageSize);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name == "删除")
            {
                if (MessageBox.Show("确认要删除单号为" + dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString() + "的借阅信息吗", "确认", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    MessageBox.Show(borowManegeService.DeleteBorrow((long)dataGridView1.Rows[e.RowIndex].Cells["id"].Value) > 0 ? "删除成功!" : "删除失败!");
                }
                GetBoorowList();
            }
            else if (dataGridView1.Columns[e.ColumnIndex].Name == "修改")
            {
                DataGridViewRow data = dataGridView1.Rows[e.RowIndex];
                //Decimal转换为Double必须用Convert.ToDouble保持精度和安全转换
                Borrow borrow = new Borrow((long)data.Cells["id"].Value, (long)data.Cells["book_id"].Value,
                    DateTime.Parse(data.Cells["start_date"].Value.ToString()), DateTime.Parse(data.Cells["end_date"].Value.ToString()), (int)data.Cells["is_still"].Value, (long)data.Cells["user_id"].Value);
                MessageBox.Show(borowManegeService.UpdateBorrow(borrow) > 0 ? "更新成功!" : "更新失败!");
                GetBoorowList();
            }
        }
        //搜索
        private void button1_Click(object sender, EventArgs e)
        {
            long borrowId = textBox1.Text.Trim() != "" ? long.Parse(textBox1.Text.Trim()) : 0;
            int isStill = checkBox1.Checked ? 1 : 0;
            DateTime startDate = DateTime.Parse(dateTimePicker1.Text.ToString());
            DateTime endDate = DateTime.Parse(dateTimePicker2.Text.ToString());
            long userId = comboBox2.SelectedIndex != -1 ? (long)comboBox2.SelectedValue : 0;
            dataGridView1.DataSource = borowManegeService.Search(borrowId, isStill, startDate, endDate, userId);
        }
        //重置搜索
        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = dateTimePicker1.Text = dateTimePicker2.Text = string.Empty;
            checkBox1.Checked = false;
            comboBox2.SelectedIndex = -1;
            GetBoorowList();
        }
        //页码跳转
        private void button3_Click(object sender, EventArgs e)
        {
            pageNum = int.Parse(textBox3.Text.Trim());
            GetBoorowList();
        }
        //上一页
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (pageNum == 1)
            {
                linkLabel1.Enabled = false;
            }
            else
            {
                linkLabel2.Enabled = true;
                --pageNum;
                textBox3.Text = pageNum.ToString();
                GetBoorowList();
            }
        }
        //下一页
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (borowManegeService.GetCount() - pageNum * pageSize < 0)
            {
                linkLabel2.Enabled = false;
            }
            else
            {
                linkLabel1.Enabled = true;
                ++pageNum;
                textBox3.Text = pageNum.ToString();
                GetBoorowList();
            }
        }
        //显示行数
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            pageNum = 1;
            pageSize = (comboBox1.SelectedIndex + 1) * 10;
            GetBoorowList();
        }
    }
}
